package com.example.teaknower.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.SearchView;

import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.teaknower.R;
import com.example.teaknower.adapter.RecycleViewAdapter;
import com.example.teaknower.model.Tea;

import com.example.teaknower.utils.OkHttpUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SearchviewActivity extends AppCompatActivity {
    private String keyword;//关键词
    private int flag ;//da.size值做是否空
    private RecyclerView recyclerView;//声明RecyclerView
    private RecycleViewAdapter recycleViewAdapter;//声明适配器
    private Context context;
    private List<Tea> mdata = new ArrayList<>();
    private MainActivity MainActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchview);
        //Toast.makeText(SearchviewActivity.this, "我就是你点击的searchview后的反应", Toast.LENGTH_SHORT).show();

        SearchView searchView = (SearchView)findViewById(R.id.searchView1);

        //setToolBar();

        recyclerView = (RecyclerView) findViewById(R.id.recycler_search);






        //
       getDate();
        recycleViewAdapter = new RecycleViewAdapter(R.layout.home_item_view, mdata);
        MainActivity = com.example.teaknower.activity.MainActivity.getMainActivity();
        recycleViewAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                //Toast.makeText(MainActivity,"点击文章的id为 ： " + mdata.get(position).getId(),Toast.LENGTH_LONG).show();
                Log.e("搜索中点击文章的id",""+mdata.get(position).getUrl());
                Intent intent = new Intent(SearchviewActivity.this, ZhiDaoDetailsActivity.class);
                intent.putExtra("url",mdata.get(position).getUrl());
                MainActivity.startActivity(intent);
            }
        });
//        /*
//        与ListView效果对应的可以通过LinearLayoutManager来设置
//        与GridView效果对应的可以通过GridLayoutManager来设置
//        与瀑布流对应的可以通过StaggeredGridLayoutManager来设置
//        */
//        //LinearLayoutManager manager = new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false);
//        LinearLayoutManager manager = new LinearLayoutManager(context);
//        manager.setOrientation(LinearLayoutManager.VERTICAL);
//        //RecyclerView.LayoutManager manager = new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false);
//        //GridLayoutManager manager1 = new GridLayoutManager(context,2);
//        //manager1.setOrientation(GridLayoutManager.VERTICAL);
//        //StaggeredGridLayoutManager manager2 = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
//        recyclerView.setLayoutManager(manager);
//        recyclerView.setAdapter(recycleViewAdapter);



        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {

                keyword = query;
                search();
                initview();
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {

                return false;
            }
        });

    }

    public void getDate() {



        String s = null;
        try {
            s = OkHttpUtil.getRequest("http://192.168.3.25:8080/Tea/findByKey?key=" + keyword);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject;
        jsonObject = JSONObject.parseObject(s);

        System.out.println("data = " + jsonObject.get("data"));
        Map<String, Object> map = (Map<String, Object>)jsonObject.get("data");
        List<Object> da = (List<Object>)map.get("content");
        System.out.println("da = " + da);
        System.out.println("da.size = " + da.size());//da.size为0可以证明无数据
        mdata.clear();
        for(int i = 0 ; i < da.size() ; i++ ){
            Map<String,Object> m = (Map<String,Object>) da.get(i);
            String id = m.get("id").toString();
            String url = m.get("url").toString();
            String wine_brand = m.get("tea_brand").toString();
            String wine_price = m.get("tea_price").toString();
            String wine_sort = m.get("tea_sort").toString();
            String wine_img = m.get("tea_img").toString();
            String wine_title = m.get("tea_title").toString();
            Tea tea = new Tea(id, url, wine_brand, wine_price, wine_sort, wine_img, wine_title);
            mdata.add(tea);
        }
    }

    public void initview(){
        LinearLayoutManager manager = new LinearLayoutManager(context);
       manager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(recycleViewAdapter);

    }


    /*
    实现那个我想搜索功能实现的逻辑？
     */
    public void search(){
        getDate();

    }


}
